﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RLZY.others
{
    class Msg
    {
        
    
        private class CloseState
        {
            private int _Timeout;

            /// <summary>
            /// In millisecond
            /// </summary>
            public int Timeout
            {
                get
                {
                    return _Timeout;
                }
            }

            private string _Caption;

            /// <summary>
            /// Caption of dialog
            /// </summary>
            public string Caption
            {
                get
                {
                    return _Caption;
                }
            }

            public CloseState(string caption, int timeout)
            {
                _Timeout = timeout;
                _Caption = caption;
            }
        }
        #region 提示框 自动关闭
       public static void DisplayMsg(string sCaption, string sMsg, int timeout = 0)
       {
           //XtraMessageBox.Show(sMsg, sCaption);
           ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox), new CloseState(sCaption, (timeout < 1000) ? 1000 : timeout)); // timeout,1000是毫秒
           sMsg = sMsg.Replace("!", "").Replace("！", "") + "!";
           MessageBox.Show(sMsg, sCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2);
       }
       private static void CloseMessageBox(object state)
       {
           CloseState closeState = state as CloseState;
           Thread.Sleep(closeState.Timeout);
           IntPtr dlg = FindWindow(null, closeState.Caption);

           if (dlg != IntPtr.Zero)
           {
               IntPtr result;
               EndDialog(dlg, out result);
           }
       }
       [DllImport("user32.dll", SetLastError = true)]
       static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
       [DllImport("user32.dll")]
       static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);
       #endregion

    
    }
}
